package com.ellricky.baselib.ui.student;

import android.view.View;
import android.widget.RadioButton;

import com.andr.common.http.callback.JOCallback;
import com.andr.common.util.EmptyUtil;
import com.andr.common.util.StrUtil;
import com.andr.common.util.ToastUtil;
import com.ellricky.baselib.BaseConstant;
import com.ellricky.baselib.R;
import com.ellricky.baselib.entity.IdNamePickerEntity;
import com.ellricky.baselib.http.Api;
import com.ellricky.baselib.http.HttpUtils;
import com.ellricky.baselib.ui.base.BaseTakePhotoActivity;
import com.ellricky.baselib.dialog.picker.EPickerEntityListener;
import com.ellricky.baselib.dialog.picker.EPickerStrListener;
import com.ellricky.baselib.dialog.picker.EPickerType;
import com.ellricky.baselib.dialog.picker.EPickerUtil;
import com.ellricky.baselib.util.EAppUtil;
import com.ellricky.baselib.widget.AvatarView;
import com.ellricky.baselib.widget.EditItemView;
import com.flyco.roundview.RoundTextView;

import org.devio.takephoto.model.TResult;
import org.json.JSONObject;

import java.io.File;
import java.util.*;


public class StudentAddActivity extends BaseTakePhotoActivity implements EPickerStrListener {

    private AvatarView avatarView;

    private EditItemView etName;
    private EditItemView etBirth;
    private EditItemView etEnterSchool;
    private EditItemView etTel;
    private EditItemView etAddr;
    private EditItemView etVip;
    private EditItemView etMoney;
    private EditItemView etHourNum;

    private RadioButton rbMan, rbWoman;

    private RoundTextView btnSubmit;


    private EPickerUtil ePickerUtil;
    private IdNamePickerEntity selVipCard;

    @Override
    public int getContentLayout() {
        return R.layout.base_activity_stu_add;
    }

    @Override
    public void initUIView() {
        super.initUIView();
        ePickerUtil = new EPickerUtil(this);
        mTvTitle.setText(R.string.base_add_vip);

        avatarView = findViewById(R.id.avatarView);

        etName = findViewById(R.id.etName);
        etBirth = findViewById(R.id.etBirth);
        etEnterSchool = findViewById(R.id.etEnterSchool);
        etTel = findViewById(R.id.etTel);
        etAddr = findViewById(R.id.etAddr);
        etVip = findViewById(R.id.etVip);
        etMoney = findViewById(R.id.etMoney);
        etHourNum = findViewById(R.id.etHourNum);

        rbMan = findViewById(R.id.rbMan);
        rbWoman = findViewById(R.id.rbWoman);

        btnSubmit = findViewById(R.id.btnSubmit);


        avatarView.initBg(30f);
        etTel.setInputPhone();
        etMoney.setInputNumber();
        etHourNum.setInputNumber();

        avatarView.setListener(this);
        etBirth.setOnClickListener(this);
        etEnterSchool.setOnClickListener(this);
        etVip.setOnClickListener(this);

        btnSubmit.setOnClickListener(this);

    }


    @Override
    protected void onClickView(View v) {
        super.onClickView(v);
        if (v == etBirth) {
            String birthDate = etBirth.getValue();
            ePickerUtil.showBirthTimePicker(birthDate, this);
            return;
        }
        if (v == etEnterSchool) {
            String enterSchoolDate = etEnterSchool.getValue();
            ePickerUtil.showStartDatePicker(enterSchoolDate, this);
            return;
        }
        if (v == etVip) {
            ePickerUtil.checkStuVipPicker(selVipCard, new EPickerEntityListener<IdNamePickerEntity>() {
                @Override
                public void onSelectItem(int pickType, IdNamePickerEntity idNamePickerEntity) {
                    selVipCard = idNamePickerEntity;
                    etVip.setValue(idNamePickerEntity.getName());
                }
            });
            return;
        }

        if (v == btnSubmit) {
            postSubmit();
        }

    }


    @Override
    public void onSelectItem(String content, int picPos, int pickType) {
        if (pickType == EPickerType.PICK_BIRTH_DATE) etBirth.setValue(content);
        else if (pickType == EPickerType.PICK_START_DATE) etEnterSchool.setValue(content);
    }


    private void postSubmit() {
        String name = etName.getValue();
        if (EmptyUtil.isEmpty(name)) {
            ToastUtil.show(this, R.string.base_input_name);
            return;
        }

        String sex = "";
        if (rbMan.isChecked()) sex = "1";
        else if (rbWoman.isChecked()) sex = "2";
        if (EmptyUtil.isEmpty(sex)) {
            ToastUtil.show(this, R.string.base_select_sex);
            return;
        }

        String birthday = etBirth.getValue();
        if (EmptyUtil.isEmpty(birthday)) {
            ToastUtil.show(this, R.string.base_select_birthday_date);
            return;
        }

        String fromDate = etEnterSchool.getValue();
        if (EmptyUtil.isEmpty(fromDate)) {
            ToastUtil.show(this, R.string.base_select_enter_school_date);
            return;
        }

        String tel = etTel.getValue();
        if (EmptyUtil.isEmpty(tel)) {
            ToastUtil.show(this, R.string.base_input_tel);
            return;
        }

        String address = etAddr.getValue();
        if (EmptyUtil.isEmpty(address)) {
            ToastUtil.show(this, R.string.base_input_home_address);
            return;
        }

        String vipCard = etVip.getValue();
        if (EmptyUtil.isEmpty(vipCard)) {
            ToastUtil.show(this, R.string.base_vip_type_select);
            return;
        }

        String money = etMoney.getValue();
        if (EmptyUtil.isEmpty(money)) {
            ToastUtil.show(this, R.string.base_money_goon_input);
            return;
        }


        String url = EAppUtil.getApiHost() + Api.API_STUDENT_ADD;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        paramMap.put("birthday", birthday);
        paramMap.put("fromDate", fromDate);
        paramMap.put("tel", tel);
        paramMap.put("sex", sex);
        paramMap.put("address", address);
        paramMap.put("vipCardId", selVipCard.getId());
        paramMap.put("price", money);

        String classNum = etHourNum.getValue();
        String picUrl = avatarView.getUrl();
        if (!EmptyUtil.isEmpty(picUrl))
            paramMap.put("picUrl", StrUtil.filterEmpty(picUrl));

        if (!EmptyUtil.isEmpty(classNum))
            paramMap.put("classNum", classNum);

        HttpUtils.getInstance().post(url, paramMap, new  JOCallback(this) {
            @Override
            protected void onSuccess(JSONObject jsonObject, String s) {
                setResult(BaseConstant.RESULT_CODE_ADD);
                finish();
            }
        });

    }


    @Override
    public void takeSuccess(TResult result) {
        String picUrl = result.getImage().getCompressPath();
        uploadPhoto(picUrl);
    }


    private void uploadPhoto(String picPath) {
        // 接口提交
        if (EmptyUtil.isEmpty(picPath)) {
            ToastUtil.show(this, "图片路径为空");
            return;
        }

        if (!EmptyUtil.isEmpty(picPath)) {
            String url = EAppUtil.getApiHost() + Api.API_UPLOAD_ACT_FILE;
            File file = new File(picPath);
            Map<String, String> paramMap = new HashMap<>();
            HttpUtils.getInstance().uploadPhoto(url, file, paramMap, new JOCallback(this) {
                @Override
                protected void onSuccess(JSONObject jsonObject, String s) {
                    JSONObject realDataJo = jsonObject.optJSONObject("data");
                    if (realDataJo != null) {
                        String picUrl = realDataJo.optString("url");
                        avatarView.setPic(picUrl);
                    }
                }
            });
        }
    }


}
